package com.tlkg.qa.redistest.live;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


//import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.BinaryClient.LIST_POSITION;

public class RedisUtil {

//	private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
	// Redis服务器IP
	private static String ADDR = "127.0.0.1";

	// Redis的端口号
	private static int PORT = 6379;

	// 访问密码
	private static String AUTH = "root";

	// 可用连接实例的最大数目，默认值为8；
	// 如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
	private static int MAX_ACTIVE = 1024;

	// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8。
	private static int MAX_IDLE = 200;

	// 等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时。如果超过等待时间，则直接抛出JedisConnectionException；
	private static long MAX_WAIT = -1;

	private static int TIMEOUT = 1000 * 1000;

	// 在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
	private static boolean TEST_ON_BORROW = true;

	private static JedisPool jedisPool = null;

	private static JedisPoolConfig config = new JedisPoolConfig();
	private static String[] hosts_redis;
	private static int[] ports_redis;
	private static int index = 0;
	private static int JedisPoolNum = 0;
	private static int JedisPoolNumber = 0;

	/**
	 * 初始化Redis连接池
	 */
	static {
		try {
			Properties properties = PropertiesUtil.getInstance();
			String host_redis = properties.getProperty("REDIS_IP");

			String port_redis = properties.getProperty("REDIS_PORT");

			hosts_redis = host_redis.split(",");
			String[] ports = port_redis.split(",");
			ports_redis = new int[ports.length];
			// String host_redis = "172.18.12.146";
			// int port_redis = 6379;
			for (int i = 0; i < ports.length; i++) {
				ports_redis[i] = Integer.parseInt(ports[i]);
			}

//			config.setMaxActive(MAX_ACTIVE);
//			config.setMaxIdle(MAX_IDLE);
//			config.setMaxWait(MAX_WAIT);
			config.setTestOnBorrow(TEST_ON_BORROW);
			System.out.println("***********************");
			System.out.println("redis ip:" + host_redis + "  redis port:" + port_redis);
			System.out.println("***********************");
			jedisPool = new JedisPool(config, hosts_redis[index], ports_redis[index], TIMEOUT);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// /**
	// * 初始化Redis连接池
	// */
	// static {
	// try {
	// Properties properties = PropertiesUtil.getInstance();
	// String host_redis = properties.getProperty("REDIS_IP");
	// int port_redis = Integer.parseInt(properties.getProperty("REDIS_PORT"));
	//// String host_redis = "172.18.12.146";
	//// int port_redis = 6379;
	//
	// JedisPoolConfig config = new JedisPoolConfig();
	// config.setMaxActive(MAX_ACTIVE);
	// config.setMaxIdle(MAX_IDLE);
	// config.setMaxWait(MAX_WAIT);
	// config.setTestOnBorrow(TEST_ON_BORROW);
	// // jedisPool=new JedisPool(config, ADDR, PORT, TIMEOUT);
	// System.out.println("***********************");
	// System.out.println("redis ip:" + host_redis + " redis port:" +
	// port_redis);
	// System.out.println("***********************");
	// jedisPool = new JedisPool(config, host_redis, port_redis, TIMEOUT);
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	public static void JedisPool() {
		index++;
		if (index >= hosts_redis.length) {
			index = 0;
		}
		jedisPool.destroy();
		jedisPool = new JedisPool(config, hosts_redis[index], ports_redis[index], TIMEOUT);
//		MailUtil.sendMail(
//				"redis切换ip为" + hosts_redis + ":" + hosts_redis[index] + ports_redis + ":" + ports_redis[index]);
	}

	/**
	 * 获取Jedis实例
	 * 
	 * @return
	 */
	public static Jedis getJedis() {
		try {
			if (jedisPool != null) {
				Jedis resource = jedisPool.getResource();
				return resource;
			} else {
				Jedis jedis = getJedisPool();
				return jedis;
			}
		} catch (Exception e) {
			// e.printStackTrace();
			Jedis jedis = getJedisPool();
			return jedis;
		}
	}

	public static Jedis getJedisPool() {

		if (JedisPoolNum <= 3) {
			JedisPoolNum++;
			Jedis jedis = getJedis();
			return jedis;
		} else {
			JedisPoolNum = 0;
			JedisPool();
			JedisPoolNumber++;
			if (JedisPoolNumber >= hosts_redis.length) {
				JedisPoolNumber = 0;
//				MailUtil.sendMail("此次操作没有可用redis  返回null");
				return null;
			}
			Jedis jedis = getJedis();
			return jedis;
		}

	}

	/**
	 * 获取缓存
	 * 
	 * @return
	 */
	public static String get(String name) {
		Jedis jedis = getJedis();
		String value = null;
		try {
			value = jedis.get(name);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return value;
	}

	/**
	 * 获取缓存
	 * 
	 * @return
	 */
	public static String getAndSet(String key) {
		Jedis jedis = getJedis();
		String value = null;
		try {
			value = jedis.get(key);
			if (null != value && value.length() > 0) {
				jedis.set(key, (Integer.parseInt(value) + 1) + "");
			} else {
				jedis.set(key, 100000000 + "");
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return value;
	}

	/**
	 * 从一个map中获取数据
	 * 
	 * @param key
	 * @param field
	 * @return
	 */
	public static String hGet(String key, String field) {
		Jedis jedis = getJedis();
		try {
			return jedis.hget(key, field);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 获取一个map的所有数据
	 * 
	 * @param key
	 * @param field
	 * @return
	 */
	public static Map<String, String> hGetAll(String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.hgetAll(key);
		} catch (Exception ex) {
			ex.printStackTrace();
			return new HashMap<String, String>();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 获取一个map的所有数据
	 * 
	 * @param key
	 * @param field
	 * @return
	 */
	public static Map<byte[], byte[]> hGetObjectAll(String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.hgetAll(key.getBytes());
		} catch (Exception ex) {
			ex.printStackTrace();
			return new HashMap<byte[], byte[]>();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 向一个map中存放数据
	 * 
	 * @param key
	 * @param field
	 * @param value
	 * @return
	 */
	public static String hSet(String key, String field, String value) {
		if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field) || StringUtils.isEmpty(value)) {
			return value;
		}
		Jedis jedis = getJedis();
		try {
			jedis.hset(key, field, value);
			return value;
		} catch (Exception ex) {
			ex.printStackTrace();
			return value;
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 向一个map中存放数据
	 * 
	 * @param key
	 * @param field
	 * @param value
	 * @return
	 */
	public static Object hSetObject(String key, String field, Object value) {
		if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field) || null == value) {
			return value;
		}
		Jedis jedis = getJedis();
		try {
			jedis.hset(key.getBytes(), field.getBytes(), SerializeUtil.serialize(value));
			return value;
		} catch (Exception ex) {
			System.out.println(
					"RedisUtil.hSetObject find null<key:" + key + "><field:" + field + "><value:" + value + ">");
			ex.printStackTrace();
			return value;
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 为哈希表 key 中的域 field 的值加上增量 increment
	 * 
	 * @param key
	 * @param field
	 * @return
	 */
	public static Long hincrby(String key, String field) {
		Jedis jedis = getJedis();
		long value = 0;
		try {
			value = jedis.hincrBy(key, field, 1);
			return value;

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return value;
	}

	/**
	 * 返回哈希表 key 中，所有的域和值。
	 * 
	 * @param key
	 * @return
	 */
	public static Map<String, String> hgetAll(String key) {
		Jedis jedis = getJedis();
		Map<String, String> value = new HashMap<String, String>();
		try {
			value = jedis.hgetAll(key);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return value;
	}

	/**
	 * 从哈希表中删除一个field
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static long hdel(String key, String field) {
		Jedis jedis = getJedis();
		try {
			return jedis.hdel(key, field);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0l;
	}

	/**
	 * 从redis获取Object
	 * 
	 * @param key
	 * @return
	 */
	public static Object getObject(String key) {
		Jedis jedis = getJedis();
		Object obj = null;
		try {
			byte[] objByte = jedis.get(key.getBytes());
			obj = SerializeUtil.unserialize(objByte);
		} catch (Exception ex) {
			ex.printStackTrace();
			return obj;
		} finally {
			returnResource(jedis);
		}

		return obj;
	}

	/**
	 * 设置缓存
	 * 
	 * @return
	 */
	public static String set(String key, String value) {
		Jedis jedis = getJedis();
		try {
			jedis.set(key, value);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return value;
	}

	/**
	 * 带有超时时间的redis存储
	 * 
	 * @param key
	 * @param time时间
	 *            (秒)
	 * @param value
	 * @return
	 */
	public static String setWithExpireTime(String key, Integer time, String value) {
		Jedis jedis = getJedis();
		try {
			jedis.setex(key, time, value);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return value;
	}

	/**
	 * 设置某个key的超时时间
	 * 
	 * @param key
	 * @param seconds
	 * @return
	 */
	public static Long setExpireTime(String key, int seconds) {
		Jedis jedis = getJedis();
		try {
			return jedis.expire(key, seconds);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	/**
	 * 删除缓存
	 * 
	 * @return
	 */
	public static void del(String key) {
		Jedis jedis = getJedis();
		try {
			jedis.del(key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 删除缓存
	 * 
	 * @return
	 */
	public static Long delObject(String key) {
		Jedis jedis = getJedis();
		Long back = null;
		try {
			back = jedis.del(key.getBytes());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return back;
	}

	/**
	 * redis存放object
	 * 
	 * @param key
	 * @param obj
	 * @return
	 */
	public static Object setObject(String key, Object obj) {
		Jedis jedis = getJedis();
		try {
			jedis.set(key.getBytes(), SerializeUtil.serialize(obj));
		} catch (Exception ex) {
			ex.printStackTrace();
			return obj;
		} finally {
			returnResource(jedis);
		}
		return obj;
	}

	/**
	 * redis存放带超时时间的object
	 * 
	 * @param key
	 * @param obj
	 * @param seconds
	 * @return
	 */
	public static Object setObjectWithExpireTime(String key, Object obj, int seconds) {
		Jedis jedis = getJedis();
		try {
			jedis.setex(key.getBytes(), seconds, SerializeUtil.serialize(obj));
		} catch (Exception ex) {
			ex.printStackTrace();
			return obj;
		} finally {
			returnResource(jedis);
		}
		return obj;
	}

	/**
	 * 向排序序列中加入一条记录
	 * 
	 * @param key
	 *            序列key
	 * @param score
	 *            得分
	 * @param member
	 *            元素名称
	 */
	public static void zadd(String key, double score, String member) {
		Jedis jedis = getJedis();
		try {
			jedis.zadd(key, score, member);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 有序集合中删除一个成员
	 * 
	 * @param key
	 * @param member
	 * @return 被成功移除的成员的数量，不包括被忽略的成员
	 */
	public static Long zrem(String key, String member) {
		Jedis jedis = getJedis();
		try {
			return jedis.zrem(key, member);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	/**
	 * 向排序序列中加入一组记录
	 * 
	 * @param key
	 *            序列key
	 * @param score
	 *            得分
	 * @param member
	 *            元素名称数组
	 */
	public static void zaddList(String key, double score, String[] member) {
		Jedis jedis = getJedis();
		try {
			for (int i = 0; i < member.length; i++) {
				jedis.zadd(key, score, member[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 向排序序列的一条记录上增加得分
	 * 
	 * @param key
	 *            序列key
	 * @param score
	 *            增加的分数
	 * @param member
	 *            元素名称
	 */
	public static void zincrby(String key, double score, String member) {
		Jedis jedis = getJedis();
		try {
			jedis.zincrby(key, score, member);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 获取一个元素在某序列中的名次，从小到大排名
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public static long zrank(String key, String member) {
		Jedis jedis = getJedis();
		try {
			return jedis.zrank(key, member);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0;
	}

	/**
	 * 从一个序列中获取指定数据。从小到大顺序获取
	 * 
	 * @param key
	 * @param index
	 *            开始位置
	 * @param i
	 *            结束位置
	 * @return
	 */
	public static Set<String> zrange(String key, long index, int i) {
		Jedis jedis = getJedis();
		try {
			return jedis.zrange(key, index, i);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return new HashSet<String>();
	}

	public static double zscore(String key, String member) {
		double score = 0;
		Jedis jedis = getJedis();
		try {
			return jedis.zscore(key, member);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return score;
	}

	/**
	 * 从一个序列中获取指定数据。从小到大顺序获取
	 * 
	 * @param key
	 * @param start
	 *            开始位置
	 * @param end
	 *            结束位置
	 * @return
	 */
	public static List<String> lrange(String key, Long start, Long end) {
		Jedis jedis = getJedis();
		try {
			return jedis.lrange(key, start, end);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return new ArrayList<String>();
	}

	/**
	 * 向 list的尾部插入
	 * 
	 * @param key
	 * @param valueString
	 */
	public static Long rpush(String key, String valueString) {
		Jedis jedis = getJedis();
		try {
			return jedis.rpush(key, valueString);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	/**
	 * 向 list的头部插入
	 * 
	 * @param key
	 * @param valueString
	 */
	public static Long lpush(String key, String valueString) {
		Jedis jedis = getJedis();
		try {
			return jedis.lpush(key, valueString);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	/**
	 * 从list中删除与value相同的元素
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static Long lrem(String key, String value) {
		Jedis jedis = getJedis();
		try {
			return jedis.lrem(key, 0, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	/**
	 * 从list尾部删除一个元素
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static Long lremOne(String key, String value) {
		Jedis jedis = getJedis();
		try {
			return jedis.lrem(key, -1, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	/**
	 * 将value插入到pivot后
	 * 
	 * @param key
	 * @param pivot
	 * @param value
	 * @return
	 */
	public static Long linsert_after(String key, String pivot, String value) {

		Jedis jedis = getJedis();
		try {
			return jedis.linsert(key, LIST_POSITION.AFTER, pivot, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;

	}

	public static long llen(String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.llen(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	// public static Long lindex( String key,String value ){
	// Jedis jedis = getJedis();
	// try {
	// return jedis.l
	// } catch (Exception e) {
	// e.printStackTrace();
	// } finally {
	// returnResource(jedis);
	// }
	// return 0L;
	// }

	/**
	 * 从一个序列中获取指定数据。从小到大顺序获取
	 * 
	 * @param key
	 * @param start
	 *            开始位置
	 * @param end
	 *            结束位置
	 * @return
	 */
	public static String rpop(String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.rpop(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return null;
	}

	/******************************************************************/

	public static Long lpushobject(String key, Object obj) {
		Jedis jedis = getJedis();
		try {
			return jedis.lpush(key.getBytes(), SerializeUtil.serialize(obj));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	public static Long rpushobject(String key, Object obj) {
		Jedis jedis = getJedis();
		try {
			return jedis.rpush(key.getBytes(), SerializeUtil.serialize(obj));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	public static List<Object> lrangeobject(String key) {
		Jedis jedis = getJedis();
		List<Object> list = new ArrayList<Object>();
		List<byte[]> byteList = new ArrayList<byte[]>();
		try {
			byteList = jedis.lrange(key.getBytes(), 0, -1);
			for (byte[] bs : byteList) {
				list.add(SerializeUtil.unserialize(bs));
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return list;
	}

	public static Long lremobject(String key, Object obj) {
		Jedis jedis = getJedis();
		try {
			return jedis.lrem(key.getBytes(), 0, SerializeUtil.serialize(obj));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0L;
	}

	/******************************************************************/

	/**
	 * 获取一个元素在某序列中的名次，从大到小排名
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public static long zrevrank(String key, String member) {
		Jedis jedis = getJedis();
		try {
			return jedis.zrevrank(key, member);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0;
	}

	/**
	 * 从一个序列中获取指定数据。从大到小顺序获取
	 * 
	 * @param key
	 * @param start
	 *            开始位置
	 * @param end
	 *            结束位置
	 * @return
	 */
	public static Set<String> zrevrange(String key, Long start, Long end) {
		Jedis jedis = getJedis();
		try {
			return jedis.zrevrange(key, start, end);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return new HashSet<String>();
	}

	/**
	 * 向集合中添加元素
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static long sadd(String key, String value) {
		Jedis jedis = getJedis();
		try {
			return jedis.sadd(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0l;
	}

	/**
	 * 从集合中移除元素
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static long srem(String key, String value) {
		Jedis jedis = getJedis();
		try {
			return jedis.srem(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return 0l;
	}

	/**
	 * 返回集合中所有元素
	 * 
	 * @param key
	 * @return
	 */
	public static Set<String> smembers(String key) {
		Set<String> set = new HashSet<>();

		Jedis jedis = getJedis();
		try {
			set = jedis.smembers(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return set;
	}

	/**
	 * 判断集合中 成员存不存在
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public static boolean sIsmember(String key, String member) {
		boolean b = false;
		Jedis jedis = getJedis();
		try {
			return jedis.sismember(key, member);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return b;
	}

	/**
	 * 释放jedis资源
	 * 
	 * @param jedis
	 */
	public static void returnResource(final Jedis jedis) {
		try {
			if (jedis != null) {
				jedisPool.returnResource(jedis);
			}
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		}
	}

	/**
	 * 带有超时时间的redis存储
	 * 
	 * @param key
	 * @param time时间
	 *            (秒)
	 * @param value
	 * @return
	 */
	public static void setByteWithExpireTime(String key, Integer time, Object object) {
		Jedis jedis = getJedis();
		try {
			jedis.setex(key.getBytes(), time, SerializeUtil.serialize(object));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 
	 * @param key
	 * @param member
	 * @return 返回自增值
	 */
	public static long incr(String key) {
		Jedis jedis = getJedis();
		long value = 0;
		try {
			value = jedis.incr(key);
		} catch (Exception e) {
			e.printStackTrace();
			jedis.set(key, "0");
		} finally {
			returnResource(jedis);
		}
		return value;
	}

	/**
	 * 获取一个元素在某序列中的得分
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public static Set<String> getKeys(String prefix) {
		Set<String> keys = null;
		Jedis jedis = getJedis();
		try {
			keys = jedis.keys(prefix);
			return keys;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 获取一个元素在某序列中的得分
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public static void deletekeylist(List<String> keys) {
		Jedis jedis = getJedis();
		try {
			for (String key : keys) {
				jedis.del(key);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 获取一个元素在某序列中的得分
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public static void deletekeyset(Set<String> keys) {
		Jedis jedis = getJedis();
		try {
			for (String key : keys) {
				jedis.del(key);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public static void deletekeys(String prefix) {
		Set<String> keys = null;
		Jedis jedis = getJedis();
		try {
			keys = jedis.keys(prefix);
			for (String key : keys) {
				jedis.del(key);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 获取一个元素在某序列中的得分
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public static Map<String, Object> getValuesByKeys(Set<String> keys) {
		Jedis jedis = getJedis();
		Map<String, Object> values = new HashMap<String, Object>();
		try {
			Iterator<String> iter = keys.iterator();
			while (iter.hasNext()) {
				String key = iter.next();
				values.put(key, jedis.get(key));
			}
			return values;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * @param map
	 */
	public static void setValuesAndKeys(Map<String, String> map) {
		Jedis jedis = getJedis();
		try {
			Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry<String, String> me = iter.next();
				jedis.set(me.getKey(), me.getValue());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 是否存在key，key必须是完整的，否则返回false
	 * 
	 * @param key
	 * @return true or false
	 */
	public static boolean existsKey(String key) {
		Jedis jedis = getJedis();
		boolean b = false;
		try {
			return jedis.exists(key);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return b;
	}

	/**
	 * 是否存在key，key必须是完整的，否则返回false
	 * 
	 * @param key
	 * @return true or false
	 */
	public static boolean exists(String key) {
		Jedis jedis = getJedis();
		boolean b = false;
		try {
			return jedis.exists(key);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return b;
	}

	/**
	 * 模糊查询，key是否存在
	 * 
	 * @param key
	 * @return
	 */
	public static boolean containsKey(String key) {
		Jedis jedis = getJedis();
		boolean b = false;
		Set<String> set = null;
		try {
			set = jedis.keys(key);
			if (set != null && set.size() > 0) {
				b = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return b;
	}

	/**
	 * redis 分布锁 加锁
	 * 
	 * @param key
	 * @return 成功返回true 失败一直等
	 */
	public static boolean lock(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			do {
				Long i = jedis.setnx(key, key);
				if (i == 1) {
					jedis.expire(key, 1 * 5);
					return true;
				} else {
//					logger.error("--redis util wait for lock--  " + key);
				}
				Thread.sleep(50);
			} while (true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return false;
	}

	/**
	 * @param key
	 */
	public static void unlock(String key) {
		del(key);
	}

	public static void main(String[] args) {

		String key = "testSet";

		RedisUtil.sadd(key, "127.0.0.1:9001");
		RedisUtil.sadd(key, "127.0.0.2:9002");
		RedisUtil.sadd(key, "127.0.0.3:9003");

		Set<String> set = RedisUtil.smembers(key);
		System.err.println(set.toString());

		RedisUtil.srem(key, "127.0.0.2:9002");
		set = RedisUtil.smembers(key);
		System.err.println(set.toString());
	}
}
